import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:image_picker/image_picker.dart';
import '../../helper/channelHelper.dart';
import '../../helper/routeHelper.dart';
import '../../model/cleanobjects.dart';
import '../../pages/gmap/gmap.dart';
import '../../pages/gmap/gmap2.dart';
import '../../pages/gmap/gmapHelper.dart';
import '../../widgets/ImgScanCard.dart';
import '../../widgets/cameraCard.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';

class CleanRecordPage2 extends StatefulWidget {
  @override
  CleanRecordPage2(this.cb1, this.added);
  final CleanObjects cb1;
  final VoidCallback added;

  @override
  CleanRecordPage2State createState() => new CleanRecordPage2State();
}

class CleanRecordPage2State extends State<CleanRecordPage2> {
  final picker = ImagePicker();
  final String now = Helper.DateTimeNowString();
  String location = "";
  String img = "";
  bool uploading = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  upload(BuildContext context) async {
    if (location.length < 2 || img.length < 2) {
      ChannelHelper.showToast("请先完成定位和拍摄照片！");
      return;
    }

    if (uploading) {
      ChannelHelper.showToast("上传中，请稍候");
      return;
    }

    setState(() {
      uploading = true;
    });

    widget.cb1.Img2 = img;
    widget.cb1.TM2 = now;
    widget.cb1.State = 3;
    widget.cb1.Gps2 = location;
    await CleanObjects.Update(widget.cb1);
    ChannelHelper.showToast("保存成功！");
    widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("终点登记"),
      ),
      body: uploading
          ? Center(
              child: SpinKitCircle(
              color: Colors.blue,
            ))
          : CleanRecordBody(context),
    );
  }

  CleanRecordBody(BuildContext context) => SingleChildScrollView(
        child: CleanRecordFields(context),
      );

  void getImg(BuildContext context) async {
    RouteHelper.ShowPage(context, CameraCardPage(upImg, "终点登记拍摄"));
  }

  void upImg(String xx) {
    setState(() {
      img = xx;
    });
  }

  void getLocation() async {
    var pb1 = await Helper.CheckLocationPersmission();
    if (pb1 == true) {
      var xx = await ChannelHelper.getLocation2();
      var yy = GMapHelper.getLocationStr(xx);
      setState(() {
        location = yy;
      });
    } else {
      ChannelHelper.showToast("定位失败，缺少定位权限！");
    }
  }

  void getLocation2() async {
    var pb1 = await Helper.CheckLocationPersmission();
    if (pb1 == true) {
      var xx = await ChannelHelper.getLocation2();
      var yy = GMapHelper.getLocationStr(xx);
      setState(() {
        location = yy;
      });
    } else {
      ChannelHelper.showToast("定位失败，缺少定位权限！");
    }
  }

  Widget imgCard() {
    return GestureDetector(
        onTap: () {
          if (img != null) {
            RouteHelper.ShowPage(context, ImgScanCard(img));
          }
        },
        child: Container(
            child: new AspectRatio(
              aspectRatio: 1.5 / 1.0,
              child: Helper.Base64ToImage(img),
            ),
            margin: new EdgeInsets.only(top: 6.0, bottom: 8.0),
            alignment: Alignment.topLeft));
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];

    List<Widget> list5 = <Widget>[];
    var x1 = "点击获取";
    //if(location.length>2) x1=now;
    var x2 = "点击拍摄";
    //if(img.length>2) x2=now;

    list3.add(InputWidget.getInfo("坐标登记", x1, onTap: getLocation));
    list3.add(InputWidget.getDivider());

    if (location.length > 2) {
      var p1 = GMapHelper.getLocation(location);
      list3.add(Container(
          height: 300.0,
          child: MyMap2(
            location: p1,
            layers: [GMapHelper.MyMarker(p1)],
            zoom: 16.0,
          )));
    }

    list4.add(InputWidget.getInfo("照片登记", x2, onTap: () => getImg(context)));
    list4.add(InputWidget.getDivider());
    list4.add(Text(
      "注意：拍摄照片须能看出垃圾已卸车，同时包含车牌信息。",
      style: TextStyle(color: Colors.red),
    ));
    list4.add(InputWidget.getDivider());
    list4.add(imgCard());

    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));
    list5.add(InputWidget.getButton("保存终点登记", () => upload(context)));

    list1.add(InputWidget.inCard2(list5));
    list1.add(
      SizedBox(
        height: 20.0,
      ),
    );

    return list1;
  }

  CleanRecordFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
